<?php 
class ControllerProductDownload extends Controller {  
	public function index() { 
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('product/download', '', 'SSL');

			$this->redirect($this->url->link('account/login', '', 'SSL'));
		}
		$this->language->load('product/download');

		$this->document->setTitle($this->language->get('heading_title'));

      	$this->data['breadcrumbs'] = array();

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),        	
        	'separator' => false
      	); 

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_account'),
			'href'      => $this->url->link('account/account', '', 'SSL'),       	
        	'separator' => $this->language->get('text_separator')
      	);
		
      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_downloads'),
			'href'      => $this->url->link('product/download', '', 'SSL'),       	
        	'separator' => $this->language->get('text_separator')
      	);
      	
      	$this->download();
      	
      	if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/download.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/product/download.tpl';
		} else {
			$this->template = 'default/template/product/download.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'		
		);
  	}
  	
	public function download() {
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('product/download', '', 'SSL');

			$this->redirect($this->url->link('account/login', '', 'SSL'));
		}
		
		$download_info['filename'] = 'image/Laptop XUAN_AN - NOW.pdf';
		$download_info['mask'] = 'Bangbaogia.pdf';
		
		if ($download_info) {
			$file =  $download_info['filename'];
			$mask = basename($download_info['mask']);
			$mime = 'application/octet-stream';
			$encoding = 'binary';

			if (!headers_sent()) {
				if (file_exists($file)) {
					header('Pragma: public');
					header('Expires: 0');
					header('Content-Description: File Transfer');
					header('Content-Type: ' . $mime);
					header('Content-Transfer-Encoding: ' . $encoding);
					header('Content-Disposition: attachment; filename=' . ($mask ? $mask : basename($file)));
					header('Content-Length: ' . filesize($file));
				
					$file = readfile($file, 'rb');
				
					print($file);
				} else {
					exit('Error: Could not find file ' . $file . '!');
				}
			} else {
				exit('Error: Headers already sent out!');
			}
		
			//$this->model_account_download->updateRemaining($this->request->get['order_download_id']);
		} else {
			$this->redirect($this->url->link('common/home', '', 'SSL'));
		}
	}
}
?>